
import { Modal,Form, Select, Input,InputNumber,Upload,message} from 'antd';
import React, { useState,FC } from 'react';
import type { UploadChangeParam } from 'antd/es/upload';
import { InboxOutlined, LoadingOutlined, PlusOutlined } from '@ant-design/icons';
import type { RcFile, UploadFile, UploadProps } from 'antd/es/upload/interface';
const { Dragger } = Upload;

type Iprops={
    visible:boolean,
    onOk:()=>void,
    onCancel:()=>void
}
const getBase64 = (img: RcFile, callback: (url: string) => void) => {
  const reader = new FileReader();
  reader.addEventListener('load', () => callback(reader.result as string));
  reader.readAsDataURL(img);
};

const beforeUpload = (file: RcFile) => {
  const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
  if (!isJpgOrPng) {
    message.error('You can only upload JPG/PNG file!');
  }
  const isLt2M = file.size / 1024 / 1024 < 2;
  if (!isLt2M) {
    message.error('Image must smaller than 2MB!');
  }
  return isJpgOrPng && isLt2M;
};
  
const Advertising:FC<Iprops>=(props)=>{
    const {visible}=props
    const [loading, setLoading] = useState(false);
    const [imageUrl, setImageUrl] = useState<string>();
  
    const handleChange: UploadProps['onChange'] = (info: UploadChangeParam<UploadFile>) => {
      if (info.file.status === 'uploading') {
        setLoading(true);
        return;
      }
      if (info.file.status === 'done') {
        // Get this url from response in real world.
        getBase64(info.file.originFileObj as RcFile, url => {
          setLoading(false);
          setImageUrl(url);
        });
      }
    };
  
    const uploadButton = (
      <div>
        {loading ? <LoadingOutlined /> : <PlusOutlined />}
        <div style={{ marginTop: 8 }}>Upload</div>
      </div>
    );
  
    if(visible){
        return(
            <Modal title="添加广告"  okText="确认"
              cancelText="取消" open={visible} onOk={()=>{
              props.onOk()
          }} onCancel={()=>{
              props.onCancel();
          }}>

            <Form.Item label="产品编号">
            <InputNumber placeholder="宽"/>×<InputNumber placeholder="高"/>
            </Form.Item>

            <Form.Item label="所属分类" >
            <Select placeholder="请选择" >
            <Select.Option value="demo">Demo</Select.Option>
            </Select>
            </Form.Item>

            <Form.Item label="产品排序">
            <Input placeholder="数值越大排序越靠前"/>
            </Form.Item>

            <Form.Item
            name="url"
            label="链入地址URL:"
            rules={[{ required: true }, { type: 'url', warningOnly: true }, { type: 'string', min: 8 }]}
        >
            <Input placeholder="input placeholder" />


            <Upload
            name="avatar"
            listType="picture-card"
            className="avatar-uploader"
            showUploadList={false}
            action="https://www.mocky.io/v2/5cc8019d300000980a055e76"
            beforeUpload={beforeUpload}
            onChange={handleChange}
            >
            {imageUrl ? <img src={imageUrl} alt="avatar" style={{ width: '100%' }} /> : uploadButton}
            </Upload>

            <Dragger {...props}>
            <p className="ant-upload-drag-icon">
            <InboxOutlined />
            </p>
            <p className="ant-upload-text">Click or drag file to this area to upload</p>
            <p className="ant-upload-hint">
            Support for a single or bulk upload. Strictly prohibit from uploading company data or other
            band files
            </p>
            </Dragger>
            </Form.Item>


           
            </Modal>
          )
    }else{
        return null;
    }
}
export default Advertising